package com.hl.wechat.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.hl.wechat.demo.service.OkHttpService;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@Service
public class OkHttpServiceImpl implements OkHttpService {

    @Autowired
    OkHttpClient okHttpClient;

    @Override
    public <T> T withGet(String url, Map<String, String> map, Class<T> cls) {
        Request request =new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        try {
            return JSON.parseObject(call.execute().body().string(),cls);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
